package com.glodon.paas.account.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glodon.paas.account.api.bean.AccountConstant;
import com.glodon.paas.account.api.bean.User;
import com.glodon.paas.account.service.UserService;
import com.glodon.paas.exception.PaasException;
import com.glodon.paas.util.UUIDUtil;

@Controller
public class ProjectController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/project/{userId}/{projectId}/{roleId}/{orgId}", method = RequestMethod.POST)
    public void addProjectRole(@PathVariable String userId, @PathVariable String projectId, @PathVariable String roleId,@PathVariable String orgId,
            Map<String, Object> model,HttpServletRequest request) throws PaasException {
//        User user = userService.getUserByAccountName(email, AccountConstant.ACCOUNT_NAME_EMAIL,
//                AccountConstant.USER_STATUS_ACTIVE);
//        if (user == null)
//            throw new PaasException("user not found");
        String id = UUIDUtil.getUUIDStr();
        userService.addProjectRole(id,userId, projectId, roleId,orgId);
        model.put("user", userService.getUserById(userId));
    }
    @RequestMapping(value = "/project/{userId}/{projectId}/{roleId}/{orgId}", method = RequestMethod.DELETE)
    public void removeProjectRole(@PathVariable String userId, @PathVariable String projectId, @PathVariable String roleId,@PathVariable String orgId,
            Map<String, Object> model,HttpServletRequest request) throws PaasException {
//    	String userId=null;
    	if(StringUtils.isEmpty(userId)||"null".equalsIgnoreCase(userId)){
//    		 User user = userService.getUserByAccountName(email, AccountConstant.ACCOUNT_NAME_EMAIL,
//    	                AccountConstant.USER_STATUS_ACTIVE);
    		 userId=null;
    	}
    	if("null".equalsIgnoreCase(projectId)){
    		projectId=null;
    	}
    	if("null".equalsIgnoreCase(roleId)){
    		roleId=null;
    	}
    	if("null".equalsIgnoreCase(orgId)){
    	    orgId=null;
        }
        int result= userService.deleteProjectRole(userId, projectId, roleId,orgId);
        model.put("result", result);
    }
    @RequestMapping(value = "/project/{projectId}/{oldRoleId}/update/{newRoleId}", method = RequestMethod.POST)
    public void updateProjectRole(@PathVariable String projectId, @PathVariable("oldRoleId") String roleId,@PathVariable("newRoleId") String newRoleId,
            Map<String, Object> model,HttpServletRequest request) throws PaasException {
        int result = userService.updateProjectRole(projectId, roleId,newRoleId);
        model.put("result", result);
    }
}
